var childNodeStr = sessionStorage.getItem("childNode");
var edit = "add";//用于表示新增/修改
var rowNo;//列表的行号
/*加载多任务节点的属性信息*/
function loadMultiTask(){
	var properties = JSON.parse(sessionStorage.getItem('properties'));
	if(properties == null){
		return;
	}
	//查看页面
	if(properties[currentNodeId]){
		var viewPageId = properties[currentNodeId].viewPageId;
		if(viewPageId){
			$.ajax({
				url:path + '/flow/workPage/getPageNameById',
				type:'post',
				dataType:'json',
				data:{viewPageId:viewPageId},
				success:function(page){
					$("#viewPageName").ligerComboBox().setText(page.viewPageName);
					$("#viewPageId").val(viewPageId);
				}
			});
		}
		
		//复选框
		var allowStop = properties[currentNodeId].allowStop;
		var allowRecall = properties[currentNodeId].allowRecall;
		var allowHang = properties[currentNodeId].allowHang;
		if(allowStop == 1){
			$("#allowStop").attr("checked",true);
		}
		if(allowRecall == 1){
			$("#allowRecall").attr("checked",true);
		}
		if(allowHang == 1){
			$("#allowHang").attr("checked",true);
		}
	}

	//子任务列表
	if(childNodeStr!=undefined && childNodeStr!=""){
		allChildTasks = JSON.parse(childNodeStr);
	}
	for(var key in allChildTasks){
		debugger
		if(key.indexOf(currentNodeId)!=-1){
			var nextNodeValue = allChildTasks[key].nextNodeId;
			var workName = allChildTasks[key].name;
			var preCondition = allChildTasks[key].preCondition;
			var nextNodeName = '';
			if(nodeObj[nextNodeValue]){
				nextNodeName = nodeObj[nextNodeValue].name;
			}
			var manager = $("#mainGrid").ligerGetGridManager();
			var firstRow = manager.getRow(0);
			manager.addRow({
				id:key,
				childNodeId:nextNodeValue,
				workTasks:workName,
				preCondition: preCondition,
				nextNode:nextNodeName
			},firstRow, true);
		}
	}
}
/*子任务弹窗*/
var allChildTasks={};
function addChildTask(id){
	var mydate = new Date();
	sessionStorage.setItem("allChildTasks",JSON.stringify(allChildTasks));
	parent.$.ligerDialog.open({
		title: "新增/修改子任务",
		width:730,height:550,
		allowClose: false,
		url: path+'/flow/childTask?processId='+processId+'&childNodeId='+id,
		buttons: [
			{ text: '确定', onclick: function (item, dialog) {
				var nodeJson = dialog.frame.createJson();
				if(dialog.frame.fkId() == ""){
				parent.$.ligerDialog.warn("流程执行人不能为空");
				return;
				}
				if(dialog.frame.isNoticesNull){
					var nextNodeName = dialog.frame.$("#node").find("option:selected").text();
					var nextNodeValue = dialog.frame.$("#node").val();
					var manager = $("#mainGrid").ligerGetGridManager();
					conditions=[];
					conditions = dialog.frame.expressNodeId();
					var childTasksKey = currentNodeId+"-"+(mydate.getTime());
					if(edit != "update"){
						var firstRow = manager.getRow(0);
						if(nodeJson.name!="" && nextNodeName != ""){
							allChildTasks[childTasksKey] = nodeJson;
							debugger
							manager.addRow({
								id:childTasksKey,
								childNodeId:nextNodeValue,
								workTasks:nodeJson.name,
								preCondition:nodeJson.preCondition,
								nextNode:nextNodeName
							}, firstRow, true);
						}
						edit = "add";
					}else{
						allChildTasks[id] = nodeJson;
						manager.records[rowNo]['childNodeId'] = nextNodeValue;
						manager.records[rowNo]['workTasks'] = nodeJson.name;
						manager.records[rowNo]['preCondition'] = nodeJson.preCondition;
						manager.records[rowNo]['nextNode'] = nextNodeName;
						manager.reRender();
					}
					dialog.close();
				}
			}},
			{ text: '关闭', onclick: function (item, dialog) {
				edit = "add";
				dialog.close();
			}}
		]
	});
}
/*工具栏*/
function toolbar() {
	$("#mainGrid").ligerGetGridManager()._onResize();
}

/*获取所有子节点的Id*/
var conditions=[];
function getAllChildNodeId(){
	var childNodeIds=[];
	var manager = $("#mainGrid").ligerGetGridManager();
	var rows = manager.getData();
	if (rows && rows.length > 0) {
		$(rows).each(function () {
			childNodeIds.push(this.childNodeId);
		});
	}
	for(var i=0;i<conditions.length;i++){
		childNodeIds.push(conditions[i]);
	}
	return childNodeIds;
}

/*删除子任务*/
function deleteChildTask(){
	var manager = $("#mainGrid").ligerGetGridManager();
	var rows = manager.getData();
	if (rows && rows.length > 0) {
		$(rows).each(function () {
			allChildTasks[this.id] = "";
		});
	}
	/*判断： 把没有值的属性删除*/
	for(var k in allChildTasks){
		if(allChildTasks[k]==undefined || allChildTasks[k] == "" || allChildTasks[k] == null || JSON.stringify(allChildTasks[k]) == "{}"){
			delete allChildTasks[k];
		}
	}
	manager.deleteSelectedRow();
}

/*编辑子任务*/
function editChildTask(){
	edit = "update";
	var manager = $("#mainGrid").ligerGetGridManager();
	var rows = manager.getCheckedRows();
	var id ="";
	if (rows && rows.length > 0) {
		$(rows).each(function () {
			rowNo = this.__id;
			id = this.id;
		});
		addChildTask(id);
	}
}

var nodeStr = sessionStorage.getItem("nodeStr");
var nodeObj = JSON.parse(nodeStr);

/*创建多任务节点的json对象*/
function createJson(){
	var allowStop = 0;
	var allowRecall = 0;
	var allowHang = 0;
	if($("#allowStop").get(0).checked){
		allowStop = 1;
	}
	if($("#allowRecall").get(0).checked){
		allowRecall = 1;
	}
	if($("#allowHang").get(0).checked){
		allowHang = 1;
	}
	var fkType="";
	//定义多任务节点json对象的模型，并赋值
	var ret = {type:nodeObj[currentNodeId].type,fkId:currentUser,fkType:userType,viewPageId:$("#viewPageId").val(),
			allowStop:allowStop, allowRecall:allowRecall, allowHang:allowHang, tasks:allChildTasks};
	/*判断： 把没有值的属性删除*/
	for(var k in ret){
		if(ret[k]==undefined || ret[k] == "" || ret[k] == null || JSON.stringify(ret[k])=="{}"){
			delete ret[k];
		}
	}
	/*把json对象转换成json字符串*/
	return ret;
}
$(function(){
	$("input[type=button]").css({"background":"#ffffff","display":"inline-block","cursor":"pointer"});
	/*绑定查看页面下拉框*/
	$("#viewPageName").ligerComboBox({
		url:path + '/flow/workPage/getComboBoxData?processId='+processId+'&type=1',
		valueField: 'id',
		textField: 'pageName',
		valueFieldID: 'viewPageId',
		selectBoxWidth: 170,
		width: 170,
		highLight:true,
		cancelable:true
	});

	$("#mainGrid").ligerGrid({
		columns: [
			{ display: 'id', name: 'id', hide: true, width: 60 },
			{ display: 'id', name: 'childNodeId', hide: true, width: 60 },
			{ display: '序号',name:'no', width: 40, render: function (rowData, rowindex, value, column, rowid, page, pagesize) { return (page - 1) * pagesize + rowindex + 1; } },
			{ display: '工作任务', name: 'workTasks', align: 'center', width: 180 },
			{ display: '启动条件', name: 'preCondition', align: 'center', width: 180 },
			{ display: '下步节点', name: 'nextNode', align: 'center', width: 160 }
		],
		width: '100%', heightDiff: -1,
		usePager:false
	});
	/*接收并初始化节点*/
	$("#currentNodeName").val(nodeObj[currentNodeId].name);
	$("#navtab1").ligerTab();
	toolbar();
	/*调用加载多任务节点的属性信息方法*/
	loadMultiTask();
});
